//
//  AddOpinionsViewController.m
//  Choice
//
//  Created by A-01 on 2020/6/16.
//  Copyright © 2020 Mac. All rights reserved.
//

#import "AddOpinionsViewController.h"

@interface AddOpinionsViewController ()

@property (nonatomic, weak)UITextField * textField;
@property (nonatomic, weak)UIButton * sureButton;

@end

@implementation AddOpinionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Options";
    self.view.backgroundColor = ZRWhiteColor;
    [self setupUI];
}

- (void)setupUI{
    
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(30, kNavHeight + 10, kScreenW - 60, 40)];
       
    textFiled.borderStyle = UITextBorderStyleRoundedRect;
    textFiled.backgroundColor = [UIColor whiteColor];
    //设置光标颜色
    textFiled.tintColor = ZRBlackColor;
    textFiled.textColor = ZRBlackColor;
    textFiled.font = [UIFont systemFontOfSize:15];
    textFiled.placeholder = @"add your options...";
    
    [textFiled addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    
    self.textField = textFiled;
    
    [self.view addSubview:self.textField];
    
    UIButton * sureButton = [UIButton new];
        
    [sureButton setTitle:@"submit" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton setBackgroundColor:[UIColor colorWithHexString:@"32AEFF"]];
    [sureButton addTarget:self action:@selector(clickFinishButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:17];
    sureButton.layer.cornerRadius = 10;
    sureButton.layer.masksToBounds = YES;
        
    self.sureButton = sureButton;
        
    [self.view addSubview:self.sureButton];
}


- (void)viewDidLayoutSubviews{
        
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(40);
        make.trailing.mas_equalTo(self.view).offset(-40);
        make.top.mas_equalTo(self.textField.mas_bottom).offset(30);
        make.height.mas_equalTo(40);
    }];
}


- (void)clickFinishButton{
    
    if (self.textField.text.length == 0) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                  message:@"Please,add your options first"
                                                                           preferredStyle:UIAlertControllerStyleAlert];
                   
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                         handler:^(UIAlertAction * action) {
                                                                             
                                                                             
                                                                         }];
                   
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        
        NSArray * arr = [[NSUserDefaults standardUserDefaults] objectForKey:self.type];
        NSMutableArray * muArr = [NSMutableArray arrayWithArray:arr];
        [muArr addObject:self.textField.text];
        [[NSUserDefaults standardUserDefaults] setObject:muArr.copy forKey:self.type];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)changedTextField:(UITextField *)textField {
    
    if (textField.text.length > 10) {

    // 对超出的部分进行剪切
        textField.text = [textField.text substringToIndex:10];
    }
}


@end
